package com.daydayup.learn;


import lombok.Getter;

import java.util.Random;
import java.util.concurrent.CountDownLatch;

/**
 * 三个销售卖SU7，求3个销售团队总体销售额
 */
public class ThreadLocalDemoV1 {

    public static void main(String[] args) throws InterruptedException {
        SU7 su7 = new SU7();
        CountDownLatch countDownLatch = new CountDownLatch(3);
        for (int i = 0; i < 3; i++) {
            new Thread(() -> {
                try{
                    for (int j = 0; j < new Random().nextInt(3)+1; j++) {
                        su7.saleTotal();
                    }
                } finally {
                    countDownLatch.countDown();
                }

            },String.valueOf(i)).start();
        }
        countDownLatch.await();
        System.out.println(Thread.currentThread().getName()+"\t销售总额:"+su7.getSaleTotal());
    }

    private static class SU7{
        @Getter
        private int saleTotal;
        public synchronized void saleTotal() {
            saleTotal++;
        }
    }
}
